package com.sugar.psych.assessment.module.business.controller.admin.medal;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.sugar.psych.assessment.framework.common.pojo.PageParam;
import com.sugar.psych.assessment.framework.common.pojo.PageResult;
import com.sugar.psych.assessment.framework.common.pojo.CommonResult;
import com.sugar.psych.assessment.framework.common.util.object.BeanUtils;
import static com.sugar.psych.assessment.framework.common.pojo.CommonResult.success;

import com.sugar.psych.assessment.framework.excel.core.util.ExcelUtils;

import com.sugar.psych.assessment.framework.apilog.core.annotation.ApiAccessLog;
import static com.sugar.psych.assessment.framework.apilog.core.enums.OperateTypeEnum.*;

import com.sugar.psych.assessment.module.business.controller.admin.medal.vo.*;
import com.sugar.psych.assessment.module.business.dal.dataobject.medal.MedalDO;
import com.sugar.psych.assessment.module.business.service.medal.MedalService;

@Tag(name = "管理后台 - 勋章信息")
@RestController
@RequestMapping("/business/medal")
@Validated
public class MedalController {

    @Resource
    private MedalService medalService;

    @PostMapping("/create")
    @Operation(summary = "创建勋章信息")
    @PreAuthorize("@ss.hasPermission('business:medal:create')")
    public CommonResult<Long> createMedal(@Valid @RequestBody MedalSaveReqVO createReqVO) {
        return success(medalService.createMedal(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新勋章信息")
    @PreAuthorize("@ss.hasPermission('business:medal:update')")
    public CommonResult<Boolean> updateMedal(@Valid @RequestBody MedalSaveReqVO updateReqVO) {
        medalService.updateMedal(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除勋章信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('business:medal:delete')")
    public CommonResult<Boolean> deleteMedal(@RequestParam("id") Long id) {
        medalService.deleteMedal(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除勋章信息")
                @PreAuthorize("@ss.hasPermission('business:medal:delete')")
    public CommonResult<Boolean> deleteMedalList(@RequestParam("ids") List<Long> ids) {
        medalService.deleteMedalListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得勋章信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('business:medal:query')")
    public CommonResult<MedalRespVO> getMedal(@RequestParam("id") Long id) {
        MedalDO medal = medalService.getMedal(id);
        return success(BeanUtils.toBean(medal, MedalRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得勋章信息分页")
    @PreAuthorize("@ss.hasPermission('business:medal:query')")
    public CommonResult<PageResult<MedalRespVO>> getMedalPage(@Valid MedalPageReqVO pageReqVO) {
        PageResult<MedalDO> pageResult = medalService.getMedalPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, MedalRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出勋章信息 Excel")
    @PreAuthorize("@ss.hasPermission('business:medal:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMedalExcel(@Valid MedalPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MedalDO> list = medalService.getMedalPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "勋章信息.xls", "数据", MedalRespVO.class,
                        BeanUtils.toBean(list, MedalRespVO.class));
    }

}